//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "tyr_menu\fn_mouseWheelRec.sqf"
#include <core_macros.h>

TRACE_1("Start",_this);
TMFPROFILERSTART;

private ["_disp","_dz","_idx","_ctrl","_ctrl2","_displayed","_current_item","_i","_current"];
_dz = _this select 1;

_disp = uiNameSpace getVariable [QGVAR(display_ui),displayNull];

_handled = false;
if (GVAR(is_open)) then {
	GVAR(MouseChange) = GVAR(MouseChange) + _dz;
	if (GVAR(SelectedItem) < 0) then {
		GVAR(SelectedItem) = 0;
	} else {
		if (GVAR(MouseChange) < - 1) then {
			GVAR(SelectedItem) = GVAR(SelectedItem) + 1;
			GVAR(MouseChange) = 0;
		};
		if (GVAR(MouseChange) > 1) then {
			GVAR(SelectedItem) = GVAR(SelectedItem) - 1;
			GVAR(MouseChange) = 0;
		};
	};
	if (GVAR(SelectedItem) >= GVAR(number_items)) then {
		GVAR(SelectedItem) = GVAR(number_items) - 1;
	};
	if (GVAR(SelectedItem) < 0) then {
		GVAR(SelectedItem) = 0;
	};
	if (GVAR(SelectedItem) >= 0) then {
		if ((GVAR(SelectedItem) - GVAR(items_offset)) >= GVAR(items_in_menu)) then {
			// We are in the case where we scrolled to the last item, if there are more items to show,
			// we'll start scrolling and thus move the last item to the before last item
			if (GVAR(number_items) > (GVAR(items_offset) + GVAR(items_in_menu))) then {
				// We are in the case where we have more items to scroll into vision
				GVAR(items_offset) = GVAR(items_offset) + 1;
				TRACE_1("New Offset",ARR_3(GVAR(items_offset), GVAR(items_in_menu), (count GVAR(accessible_items))));
				for "_i" from GVAR(items_offset) to (GVAR(items_offset) + GVAR(items_in_menu) - 1) step 1 do {
					if (_i < (count GVAR(accessible_items))) then {
						_idx = 91001 + (_i - GVAR(items_offset));
						_current = GVAR(accessible_items) select _i;
						_title = GETVAL(_current,0,"");
						_icon = GETVAL(_current,3,GVAR(DefaultIcon));
						TRACE_1("Redraw",ARR_4(_i, _idx, _title, _icon));
						_ctrl = _disp displayCtrl _idx;
						_ctrl2 = _disp displayCtrl (_idx + 100);
						_ctrl ctrlSetText _title;
						//_ctrl ctrlCommit 0;
						_ctrl2 ctrlSetText _icon;
						//_ctrl2 ctrlCommit 0;
					};
				};
				
				GVAR(SelectedItem) = GVAR(SelectedItem);
			};
		} else {
			if (GVAR(SelectedItem) <= (GVAR(items_offset)) && GVAR(items_offset) > 0) then {
				// This case is when we scroll items to the top back into sight
				GVAR(items_offset) = GVAR(items_offset) - 1;
				TRACE_1("New Offset",ARR_3(GVAR(items_offset), GVAR(items_in_menu), (count GVAR(accessible_items))));
				for "_i" from GVAR(items_offset) to (GVAR(items_offset) + GVAR(items_in_menu) - 1) step 1 do {
					if (_i < (count GVAR(accessible_items))) then {
						_idx = 91001 + (_i - GVAR(items_offset));
						_current = GVAR(accessible_items) select _i;
						_title = GETVAL(_current,0,"");
						_icon = GETVAL(_current,3,GVAR(DefaultIcon));
						TRACE_1("Redraw",ARR_4(_i, _idx, _title, _icon));
						_ctrl = _disp displayCtrl _idx;
						_ctrl2 = _disp displayCtrl (_idx + 100);
						_ctrl ctrlSetText _title;
						//_ctrl ctrlCommit 0;
						_ctrl2 ctrlSetText _icon;
						//_ctrl2 ctrlCommit 0;
					};
				};
				GVAR(SelectedItem) = GVAR(SelectedItem);
			};
		};
		_current_item = GVAR(SelectedItem) - GVAR(items_offset);
		_idx = 91001 + _current_item;
		_ctrl = _disp displayCtrl _idx;
		_ctrl2 = _disp displayCtrl (_idx + 200);
		_ctrl ctrlSetBackgroundColor GVAR(ColorBackgroundSelected);
		_ctrl2 ctrlSetBackgroundColor GVAR(ColorBackgroundSelected);
		if (GVAR(PreviousItem) != _current_item) then {
			TRACE_1("Reset Item",GVAR(PreviousItem));
			if (GVAR(PreviousItem) >= 0) then {
				_idx = 91001 + GVAR(PreviousItem);
				_ctrl = _disp displayCtrl _idx;
				_ctrl2 = _disp displayCtrl (_idx + 200);
				_ctrl ctrlSetBackgroundColor GVAR(ColorBackground);
				_ctrl2 ctrlSetBackgroundColor GVAR(ColorBackground);
			};
			GVAR(PreviousItem) = _current_item;
		};
		
		_displayed = GVAR(accessible_items) select GVAR(SelectedItem);
		if (GVAR(CONFIG_SHOW_SELECTED_OPTION) == 0) then {
			if (GVAR(CONFIG_ICON_AND_TEXT) ==0) then {
				_ctrl = _disp displayCtrl 91653;
				_ctrl ctrlSetBackgroundColor GVAR(ColorBackgroundSelected);
				_ctrl ctrlSetStructuredText parseText format[GVAR(SelectedFormat),(_displayed select 0),(_displayed select 3)];
				_ctrl ctrlShow true;
			} else {
				_ctrl = _disp displayCtrl 91654;
				_ctrl ctrlSetBackgroundColor GVAR(ColorBackgroundSelected);
				_ctrl ctrlShow true;
				_ctrl = _disp displayCtrl 91655;
				_ctrl ctrlSetText (_displayed select 3);
				_ctrl ctrlShow true;
			};
		};
	};
	_handled = true;
	TRACE_1("Change",ARR_5(_dz,GVAR(MouseChange),GVAR(SelectedItem),GVAR(number_items),GVAR(items_offset)));
} else {
	GVAR(MouseChange) = 0;
};

TRACE_1("End",_handled);
TMFPROFILERSTOP;
false;
